#include "UIBase.h"
#include "util.h"
#include "keyboardsource.h"
namespace Core
{
	namespace UI
	{
		UIBase::UIBase(const Position& pos,const Size& size)
			:m_pos(pos),m_size(size),m_isMouseIn(false),m_isLeftDown(false)
		{	
			Devices::MouseEvent& mouse=Devices::MouseSource::getInstance()->getEvent();
			m_isMouseIn=IsInRange(Position(mouse.x,mouse.y),pos,size);
			m_isLeftDown=mouse.left;
			Devices::MouseSource::getInstance()->add(this);
		}
		void UIBase::OnMouseEvent(MouseEvent& mouse)
		{
			bool isNowIn=IsInRange(Position(mouse.x,mouse.y),m_pos,m_size);
			if( !m_isMouseIn && isNowIn)
			{
				m_isMouseIn=true;
				OnMouseIn(mouse);
				return;
			}
			if( m_isMouseIn && !isNowIn)
			{
				m_isMouseIn=false;
				OnMouseOut(mouse);
				return;
			}
			if( !mouse.left && m_isLeftDown)
			{
				m_isLeftDown=false;
				OnMouseUp(mouse,Devices::KeyboardSource::getInstance()->getEvent());
				if(m_isMouseIn)
				{
					OnMouseClick(mouse,Devices::KeyboardSource::getInstance()->getEvent());
				}
			}

			if( m_isMouseIn && mouse.left && !m_isLeftDown)
			{
				m_isLeftDown=true;
				OnMouseDown(mouse,Devices::KeyboardSource::getInstance()->getEvent());
				return;
			}
			return;
		}
	}
}